/**
 * Created by yanwe on 2016/6/26.
 */

(function () {
    'use strict';
    app.controller('NewGoodsController', [
        '$scope', '$http','$modal', '$timeout','goodsClass','TipService',function ($scope, $http,$modal, $timeout,goodsClass,TipService) {

            $scope.tipService = TipService;
            $scope.GoodsCategory = {
                selected: [],
                items: null
            };
            $scope.GoodsLabels = {
                selected: [],
                items: null
            };
            //品牌
            $scope.GoodsBrands = {
                selected: [],
                items: null
            };
            //面料材质
            $scope.GoodsFmaterials = {
                selected: [],
                items:null
            };
            //里料材质
            $scope.GoodsImaterials = {
                selected: [],
                items: null
            };
            //面料品牌
            $scope.GoodsFbrands = {
                selected: [],
                items: null
            };
            //颜色
            $scope.GoodsColors = {
                selected: [],
                items:null
            };
            //尺码
            $scope.sizeItems = {
                S:false,
                M:false,
                L:false,
                XL:false,
                XXL:false,
                LL:false,
                XSL:false,
                XS:false
            };




            $scope.selectColor = function (color) {
                $scope.GoodsColors.selected.push(color);

            };

            $scope.goodsColorNum=[];

            $scope.$watch('GoodsColors', function (n, o) {
                if(n.selected.length>0){
                    for(var i=0;i< n.selected.length;i++){
                        //判断该项是否已经添加到对象当中
                        var colorNum = $scope.goodsColorNum;
                        var hasC = false;
                        for(var j=0;j<colorNum.length;j++){
                            if(colorNum[j].Name == n.selected[i]){
                                hasC = true;
                            }
                        }
                        if(!hasC){
                            var temp = {
                                id: n.selected[i],
                                Name: n.selected[i],
                                size:[]
                            }
                            for(var p in $scope.sizeItems){
                                if($scope.sizeItems[p]){
                                    var ts = {
                                        name:$scope.sizeItems[p],
                                        num:0
                                    }
                                    temp.size.push(ts);
                                }
                            }
                            $scope.goodsColorNum.push(temp);
                        }
                    }
                }
            },true);
            $scope.$watch('sizeItems', function (n, o) {
                var colorNum = $scope.goodsColorNum;
               for(var i=0;i<colorNum.length;i++){
                   for(var p in $scope.sizeItems){
                       if($scope.sizeItems[p]){
                           var size = colorNum[i].size;
                           var hasp = false;
                           for(var j=0;j<size.length;j++){
                               if(size[j].name==p){
                                   hasp=true;
                               }
                           }
                           if(!hasp){
                               var ts = {
                                   name:p,
                                   num:0
                               }
                               colorNum[i].size.push(ts);
                           }
                       }
                   }
               }
            },true)

            var init = function () {
                goodsClass.getgoodMessage().then(function () {
                    var data = goodsClass.goodsDetail;
                    console.log(data);
                    $scope.GoodsCategory.items = data.goodsCategery;
                    $scope.GoodsLabels.items = data.goodsLabel;
                    $scope.GoodsBrands.items = data.goodsBrand;
                    $scope.GoodsFmaterials.items = data.goodsFabric;
                    $scope.GoodsImaterials.items = data.goodsFabric;
                    $scope.GoodsFbrands.items = data.goodsFbrands;
                    $scope.GoodsColors.items = data.goodsColor;
                })
            }();
            //商品对象
            $scope.goods={
                name:'',//
                storagePrice:'',//
                goodsClassification:$scope.GoodsCategory.selected,//分类
                tagPrice:'',
                goodLabel:$scope.GoodsLabels.selected,//标签
                goodMessageFile:'',
                goodFabrics:$scope.GoodsFmaterials.selected,
                goodLining:$scope.GoodsImaterials.selected,
                goodPattern:'',//图案
                goodBrands:$scope.GoodsBrands.selected,//品牌
                goodElasticity:'',//弹性
                goodThickness:'',//厚度
                goodDescription:'',//描述
                goodsColorNum:$scope.goodsColorNum
            }

            //保存新商品
            $scope.saveNewGood=function(){
                //标签
                var goods = $scope.goods;
               /* console.log($scope.GoodsCategory);*/
                goods.goodsClassification = $scope.GoodsCategory.selected;
                goods.goodLabel = $scope.GoodsLabels.selected;
                goods.goodFabrics = $scope.GoodsFmaterials.selected;
                goods.goodLining = $scope.GoodsImaterials.selected;
                goods.goodBrands=$scope.GoodsBrands.selected;

                
                goodsClass.addnewGood(goods);
            }


            $scope.categoryGroupBy = function (item) {
                return item.CategoryName;
            };



            $scope.addProps=function(item){
                console.log($scope.sizeItmes);
                var modalInstance = $modal.open({
                    templateUrl: 'tpl/goods/addProps.html',
                    controller: 'addPropsCtrl',
                    size: 'lg',
                    resolve:{
                        items: function () {
                            return item;
                        }
                    }
                });
            };

            $scope.items = ['item1', 'item2', 'item3'];
            $scope.open = function (size) {
                var modalInstance = $modal.open({
                    templateUrl: 'myModalContent.html',
                    controller: 'ModalInstanceCtrl',
                    size: size,
                    resolve: {
                        items: function () {
                            return $scope.items;
                        }
                    }
                });

                modalInstance.result.then(function (selectedItem) {
                    $scope.selected = selectedItem;
                }, function () {
                    $log.info('Modal dismissed at: ' + new Date());
                });
            };

        }
    ]);
    app.controller('ModalInstanceCtrl', ['$scope', '$modalInstance', 'items', function($scope, $modalInstance, items) {
        $scope.items = items;
        $scope.selected = {
            item: $scope.items[0]
        };

        $scope.ok = function () {
            $modalInstance.close($scope.selected.item);
        };

        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
    }]);

    app.controller('newGoodsClassification', ['$scope', '$modalInstance','goodsClass',function($scope, $modalInstance,goodsClass) {

        $scope.goodsClass={
            style:'',
            age:''
        };

        $scope.ok = function () {
            //$modalInstance.close($scope.selected.item);
            goodsClass.addgoodsClass($scope.goodsClass);
        };

        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
    }]);

    app.controller('goodsLabel', ['$scope', '$modalInstance','goodsClass',function($scope, $modalInstance,goodsClass) {

        $scope.goods={
            goodLabel:''
        };

        $scope.ok = function () {
            //$modalInstance.close($scope.selected.item);
            goodsClass.addgoodsLabel($scope.goods.goodLabel);
        };

        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
    }]);

    app.controller('addPropsCtrl', ['$scope', '$modalInstance','goodsClass','items',function($scope, $modalInstance,goodsClass,item) {
    	 $scope.items={
    	            classes:{
    	                isShow:false,
    	                addValue:'',
    	                key:'classes'

    	            },
    	            label:{
    	                isShow:false,
    	                addValue:'',
    	                key:'label'
    	            },
    	            brand:{
    	                isShow:false,
    	                addValue:'',
    	                key:'brand'
    	            },
    	            fMaterial:{
    	                isShow:false,
    	                addValue:'',
    	                key:'fMaterial'
    	            },
    	            iMaterial:{
    	                isShow:false,
    	                addValue:'',
    	                key:'iMaterial'

    	            },
    	            fBrand:{
    	                isShow:false,
    	                addValue:'',
    	                key:'fBrand'
    	            },
    	            color:{
    	                isShow:false,
    	                addValue:'',
    	                key:'color'
    	            }
    	 };        
        $scope.items[item].isShow = true;

        $scope.ok = function () {
            goodsClass.addProps($scope.items[item]);
            $modalInstance.dismiss('cancel');
        };

        $scope.cancel = function () {
            $modalInstance.dismiss('cancel');
        };
    }]);

    app.controller('ModalDemoCtrl', ['$scope', '$modal', '$log', function($scope, $modal, $log) {
        $scope.items = ['item1', 'item2', 'item3'];
        $scope.open = function (size) {
            var modalInstance = $modal.open({
                templateUrl: 'myModalContent.html',
                controller: 'ModalInstanceCtrl',
                size: size,
                resolve: {
                    items: function () {
                        return $scope.items;
                    }
                }
            });
            modalInstance.result.then(function (selectedItem) {
                $scope.selected = selectedItem;
            }, function () {
                $log.info('Modal dismissed at: ' + new Date());
            });
        };
    }]);
})();
